package lk;

public class D230917 {
    // 压缩字符串
    public int compress(char[] chars) {
        int i = 0;  // 遍历 chars
        int j = 0;  // 用于写回 chars
        int n = chars.length;
        while (i < n) {
            char ch = chars[i++];
            int count = 1;  // 统计相同字符的数量
            while (i < n && ch == chars[i]) {
                i++;
                count++;
            }
            chars[j++] = ch;
            if (count != 1) {
                String s = String.valueOf(count);
                for (char c : s.toCharArray()) {
                    chars[j++] = c;
                }
            }
        }
        return j;
    }
}
